//
//  WTSquareViewController.m
//  OneDay
//
//  Created by lanou on 16/1/15.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTSquareViewController.h"
#import "WTSquareCell.h"
#import "WTRequestManager.h"
#import "WTSquareDetailViewController.h"
#import <MJRefresh.h>
#import <MBProgressHUD.h>

@interface WTSquareViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) NSMutableArray * listArray;
// 网络请求起始位置
@property (nonatomic, assign) NSInteger requsetStart;

// 判断是广场还是最热
@property (nonatomic, strong) NSString * type;

// 等待数据加载完成的动画
@property (nonatomic, strong) MBProgressHUD * hud;
@end

@implementation WTSquareViewController

// 视图将要出现时,回复tabBar
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _requsetStart = 1;
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenHeight - 64 - 49 - 30 * kRelativelength_Y) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[WTSquareCell class] forCellReuseIdentifier:@"WTSquareCell"];
    
    _listArray = [NSMutableArray array];
    
    
#pragma mark -- 明杰刷新
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _requsetStart = 1;
        [self requestData:_type];
       
    }];
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _requsetStart++;
        [self requestData:_type];
       
    }];
    
    
    [self setupHud];
    // Do any additional setup after loading the view.
}

#pragma mark -- 等待数据加载完成的动画
- (void)setupHud{
    _hud = [[MBProgressHUD alloc]initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.mode = MBProgressHUDModeIndeterminate;
    _hud.labelText = @"正在加载完成...";
    [self.view addSubview:_hud];
    [_hud show:YES];
}

#pragma mark -- 网络请求
- (void)requestData:(NSString *)type
{
  
   
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"page"] = [NSString stringWithFormat:@"%ld",(long)_requsetStart];
    dic[@"type"] = type;
    dic[@"plat"] = @"android";
    dic[@"proct"] = @"mierapp";
    dic[@"versioncode"] = @"20150807";
    dic[@"apiCode"] = @"4";

    
    
    [WTRequestManager requestWithUrlString:@"http://bbs.mier123.com/api/quanzi/hot_thread.php?plat=android&proct=mierapp&versioncode=20150807&apiCode=4" parDic:dic method:POST success:^(NSData *data) {
        NSDictionary * dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray * newsListArr = dataDic[@"newsList"];
       
        if (_requsetStart == 1) {
            [_listArray removeAllObjects];
        }
        
        
        for (NSDictionary * oneDic in newsListArr) {
            WTSquareModel * model = [[WTSquareModel alloc]init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_listArray addObject:model];
             _type = type;
        }
        
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        [_tableView reloadData];
        [_hud hide:YES];
    } error:^(NSError *error) {
        
    }];
}

#pragma mark -- 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WTSquareCell * cell = [tableView dequeueReusableCellWithIdentifier:@"WTSquareCell" forIndexPath:indexPath];
    cell.model = _listArray[indexPath.row];
    return cell;
}

#pragma mark -- 跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WTSquareModel * model = _listArray[indexPath.row];

    self.block(model.tid,model.fid);
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [WTSquareCell heightForRowWithModel:_listArray[indexPath.row]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
